﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.customer.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.hh.bss.auth.entity.SystemUser;
import com.hh.bss.base.*;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.cache.CustomerCache;
import com.hh.bss.common.cache.SystemUserCache;
import com.hh.bss.common.exception.RepeatRecordException;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.customer.dao.*;
import com.hh.bss.customer.entity.*;
import com.hh.bss.customer.service.*;
import com.hh.bss.customer.vo.query.*;
import com.hh.bss.util.*;




@Service
@Transactional
public class CustomerBdServiceImpl extends BaseBusinessService<CustomerBd,java.lang.Integer>  implements CustomerBdService<CustomerBd,java.lang.Integer>{

	private CustomerBdDao customerBdDao;
	private CustomerDao customerDao;
	private CompanyDao companyDao;
	private CompanyService companyService;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setCustomerBdDaoImpl(CustomerBdDao dao) {
		this.customerBdDao = dao;
	}
	public void setCompanyDaoImpl(CompanyDao dao) {
		this.companyDao = dao;
	}
	public void setCompanyServiceImpl(CompanyService service) {
		this.companyService = service;
	}
	public void setCustomerDaoImpl(CustomerDao dao) {
		this.customerDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.customerBdDao;
	}
	public void removeById(Integer id){
		customerBdDao.deleteById(id);
		//刷新缓存
		CustomerCache.refresh();
	}
	
	@Transactional(readOnly=true)
	public Page findPage(CustomerBdQuery query) {
		return customerBdDao.findPage(query);
	}
	public void save(CustomerBd customerBd){
		//如果已经被锁定，则抛异常
		Object[] ob={customerBd.getCustId()};
		
		
		
		List l=customerBdDao.getJdbcDao().findByQuery("select bd_account from customer_bd a where a.cust_id=? and  date(ifnull(a.expired_d,date_add(a.create_dt,interval 90 day)))>date(now())", ob,new MapObjectRowMapper());
		if (l!=null&&l.size()>0){
			//
			for (int i=0;i<l.size();i++){
				Map m=(Map)l.get(i);
				System.out.println(customerBd.getOnlineUser().getUsername()+"---"+m);
				//m.values().contains(arg0)
				if (customerBd.getOnlineUser()!=null&&m.values().contains(customerBd.getOnlineUser().getUsername())) throw  new RuntimeException("BD客户已经添加过!");;
			}
			//String uname="";
			//if (customerBd.getOnlineUser()!=null) uname=customerBd.getOnlineUser().getName();
			Map mbd=(Map)l.get(0);
			SystemUser uname=SystemUserCache.getSystemUserByUsername((String)mbd.get("bd_account"));
			String name="";
			if (uname!=null) name=uname.getName();
			throw  new RuntimeException("BD客户已经被用户[+name+]锁定，不可添加!");
			
		}
		
		
		
		customerBdDao.save(customerBd);
		
		//更新缓存
		List l2=customerBdDao.getHibernateTemplate().find("select a from Customer a where a.id=? ", ob);
		if (l2!=null&&l2.size()>0) {
			Customer customer=(Customer)l2.get(0);
			CustomerCache.addCustomerBD(customerBd, customer);			
		}
		
		
	}
	public void save(CustomerBd customerBd,Integer companyId){
		
		//如果没有customer记录，则添加
		Customer customer=new Customer();
		List l_customer=customerBdDao.getHibernateTemplate().find("select a from Customer a where a.companyId=? ",companyId);
		if (l_customer==null||l_customer.size()==0) {
			customer.setCompanyId(companyId);
			customer.setCreateDt(new Date());
			customer.setCreator(customerBd.getBdAccount());
			customerDao.save(customer);
			customerDao.flush();
		}else{
			customer=(Customer)l_customer.get(0);
		}
		
		//如果已经被锁定，则抛异常
		
		
		customerBd.setCustId(customer.getId());
		customerBd.setBdStatus(new Integer(0));
		customerBd.setExpiredD(DateHelper.add(new Date(), DateHelper.DAY, 90));
		
		Object[] ob={customerBd.getCustId()};
		
		
		
		List l=customerBdDao.getJdbcDao().findByQuery("select bd_account from customer_bd a where a.cust_id=? and  date(ifnull(a.expired_d,date_add(a.create_dt,interval 90 day)))>date(now())", ob,new MapObjectRowMapper());
		if (l!=null&&l.size()>0){
			//
			for (int i=0;i<l.size();i++){
				Map m=(Map)l.get(i);
				System.out.println(customerBd.getOnlineUser().getUsername()+"---"+m);
				//m.values().contains(arg0)
				if (customerBd.getOnlineUser()!=null&&m.values().contains(customerBd.getOnlineUser().getUsername())) throw  new RuntimeException("BD客户已经添加过!");;
			}
			//String uname="";
			//if (customerBd.getOnlineUser()!=null) uname=customerBd.getOnlineUser().getName();
			Map mbd=(Map)l.get(0);
			SystemUser uname=SystemUserCache.getSystemUserByUsername((String)mbd.get("bd_account"));
			String name="";
			if (uname!=null) name=uname.getName();
			throw  new RuntimeException("BD客户已经被用户[+name+]锁定，不可添加!");
		}
		
		
		//List l=customerBdDao.getHibernateTemplate().find("select a from CustomerBd a where a.custId=? and ", ob);
		//if (l!=null&&l.size()>0) return;
		customerBdDao.save(customerBd);
	}
	public void addComapnyBdCustomer(Company company, Customer customer) {
		// TODO Auto-generated method stub
		//判断公司是否重复
		boolean isRepeat=companyService.isCompanyRepeat(company);
		if (isRepeat) {			
			if(!StringHelper.isEmpty(company.getFullName()))
				throw new RepeatRecordException("公司["+company.getName()+"/"+company.getFullName()+"] 已经存在");	
			else throw new RepeatRecordException("公司"+company.getName()+"已经存在");
		}
		companyDao.save(company);
		companyDao.flush();
		//保存customer
		customer.setCompanyId(company.getId());
		//如果不存在，则处理
		List l2=customerBdDao.getHibernateTemplate().find("select a from Customer a where a.companyId=? ", company.getId());
		if (l2==null||l2.size()==0) {
			customerDao.save(customer);			
		}else {
		  Customer oldCustomer=(Customer)l2.get(0);
		  customer.setId(oldCustomer.getId());
		}
		
		customerDao.flush();
		CustomerBd customerBd=new CustomerBd();
		customerBd.setCreateDt(new Date());
		customerBd.setBdAccount(customer.getCreator());
		customerBd.setCustId(customer.getId());
		customerBd.setBdStatus(new Integer(0));
		customerBd.setExpiredD(DateHelper.add(new Date(), DateHelper.DAY, 90));
		customerBdDao.save(customerBd);
		customerBdDao.flush();
	}
	
}
